//
//  LoginRegistViewController.m
//  999-ChatBike
//
//  Created by 李晓东 on 2018/4/15.
//  Copyright © 2018年 PeanutXu. All rights reserved.
//

#import "LoginRegistViewController.h"
#import "AppDelegate.h"

@interface LoginRegistViewController () <LoginRegistDelegate>
@property (weak, nonatomic) IBOutlet UITextField *portView;

@end

@implementation LoginRegistViewController{
    AppDelegate *_app;
    NSMutableArray<UITextField*>* _fields;
}

static CGFloat viewWidth;

- (void)loadView {
    [super loadView];
    viewWidth = self.view.frame.size.width;
    _fields = [NSMutableArray arrayWithCapacity:3];
    NSArray *hints = @[@"请输入用户名", @"请输入密码", @"请输入服务器地址"];
    for (int i = 0; i < hints.count; i++) {
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 20 + i * 50, viewWidth - 20, 30)];
        [textField setBorderStyle:UITextBorderStyleRoundedRect];
        [textField setPlaceholder:hints[i]];
        [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [textField setKeyboardType:UIKeyboardTypeAlphabet];
        [self.view addSubview:textField];
        [_fields addObject:textField];
    }
    
    NSArray *titles = [NSArray arrayWithObjects:@"登录", @"注册", nil];
    SEL action[] = {@selector(loginTapped:), @selector(registerTapped:)};

    for (int i = 0; i < titles.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setFrame:CGRectMake((viewWidth - 120 * 2) / 4 * (i * 2 + 1) + 120 * i, 170, 120, 50)];

        [btn setTitle:titles[i] forState:UIControlStateNormal];
        [btn addTarget:self action:action[i] forControlEvents:UIControlEventTouchUpInside];
        CALayer *layer = btn.layer;
        layer.borderColor = [UIColor greenColor].CGColor;
        layer.borderWidth = 2;
        layer.cornerRadius = 5;
        layer.masksToBounds = YES;
        
        
        [self.view addSubview:btn];

    }
    
    _app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    _app.loginRegistDelegate = self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)loginTapped:(id)sender {
    [self loginOrRegist:NO];
    [_fields[2] resignFirstResponder];
}

- (void)registerTapped:(id)sender {
    [self loginOrRegist:YES];
}

- (void)loginOrRegist:(BOOL)isRegist {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString* userText = _fields[0].text;
    NSString* passText = _fields[1].text;
    NSString* serverText = _fields[2].text;
    
//    if (userText != nil && passText != nil && serverText != nil
//        && userText.length > 0 && passText.length > 0 && serverText.length > 0) {
        [userDefaults setObject:userText forKey:USER_KEY];
        [userDefaults setObject:passText forKey:PASS_KEY];
        [userDefaults setObject:serverText forKey:SERVER_KEY];
        [userDefaults setObject:self.portView.text forKey:@"port"];
        [userDefaults synchronize];
        [_app connect:isRegist];
        
//    } else {
//
//    }
}

- (void)loginSuccess{
    dispatch_async(dispatch_get_main_queue(), ^{
        id mainScene = [self.storyboard instantiateViewControllerWithIdentifier:@"mainScene"];
        [self presentViewController:mainScene animated:YES completion:nil];
    });
    
}

@end
